package com.xhwl.logistics.strategy.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.dto.http.DeviceInfoDaHuaIccDTO;
import com.xhwl.logistics.service.business.VehicleDeviceDataPushBusinessService;
import com.xhwl.logistics.service.impl.http.DaHuaHttpServiceImpl;
import com.xhwl.logistics.service.impl.http.HongMenHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotDeviceHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 大华获取设备相关信息
 */
@Slf4j
//@Service
public class VehicleParkingLotDaHuaDeviceHandlerImpl implements IVehicleParkingLotDeviceHandler {

    @Autowired
    private DaHuaHttpServiceImpl daHuaHttpService;

    @Autowired
    private VehicleDeviceDataPushBusinessService vehicleDeviceDataPushBusinessService;

    @Autowired
    private HongMenHttpServiceImpl hongMenHttpService;

    @Override
    public void syncDeviceWithStatus(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        log.info("DaHua sync device status start!");

        List<DeviceInfoDaHuaIccDTO> daHuaDevices = daHuaHttpService.getDaHuaDevice(vehicleIntegratedConfigBO);

        daHuaDevices.forEach(deviceInfoDaHuaIccDTO -> {
            vehicleDeviceDataPushBusinessService.usualDeviceStatus(deviceInfoDaHuaIccDTO.getDeviceIp(), deviceInfoDaHuaIccDTO.getIsOnline(),
                    vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId());
        });

        //查看这个大华ICC的集成配置中是否存在双厂商的定制化需求
        if(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams()!=null){
            JSONObject customParamsObj = JSONObject.parseObject(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getCustomParams());
            if(customParamsObj.getBoolean("doubleFunContrMn")){
                JSONArray channelArr = hongMenHttpService.queryDeviceByPassage(customParamsObj.getString("host"), null);
                for (Object object2 : channelArr) {
                    JSONObject channelObj = JSONObject.parseObject(object2.toString());
                    JSONArray deviceArr = JSONObject.parseArray(channelObj.getString("equipments"));
                    for (Object object3 : deviceArr) {
                        JSONObject deviceObj = JSONObject.parseObject(object3.toString());
                        vehicleDeviceDataPushBusinessService.usualDeviceStatusByThirdDeviceId(deviceObj.getString("id"),
                                this.convertHongMenDeviceStatus(deviceObj.getString("state")), vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId());
                    }
                }
            }
        }

        log.info("DaHua sync device status end!");
    }


    /**
     * 转换红门车行的设备状态
     */
    private String convertHongMenDeviceStatus(String deviceStatus){
        switch (deviceStatus){
            case "CONNECTED":
                return "1";
            default:
                return "0";
        }
    }
}
